package blog.com.mw.base;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import blog.com.mw.pojo.po.ArticlePO;
import blog.com.mw.pojo.vo.ArticleVO;

public class BaseVO {

	/**
	 * 通过反射的形式把PO类转化为VO类
	 * @param <T>
	 * @param po
	 * @return
	 */
	public <T> BaseVO poToVo(T po) {
		Class clsPo = null;
		Class clsVo = null;
		Field[] poFiles = null;
		Field[] voFiles = null;
		try {
			if(po == null) {
				return null;
			}
			clsPo = po.getClass();
			clsVo = this.getClass();
			poFiles = clsPo.getDeclaredFields();
			voFiles = clsVo.getDeclaredFields();
			for(Field item:poFiles) {
				item.setAccessible(true);
				if(item.get(po) == null) {
					continue;
				}
				for(Field field:voFiles) {
					field.setAccessible(true);
					if(field.getName().equals(item.getName())) {
						if(item.getType().equals(Date.class)) {
							field.set(this, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(item.get(po)));
						}else {
							field.set(this, String.valueOf(item.get(po)));
						}
					}
				}
				
			}
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return this;
	}
	
	
	public <T> T voToPo(T po) {
		Class clsPo = null;
		Class clsVo = null;
		Field[] poFiles = null;
		Field[] voFiles = null;
		try {
			clsPo = po.getClass();
			clsVo = this.getClass();
			poFiles = clsPo.getDeclaredFields();
			voFiles = clsVo.getDeclaredFields();
			for(Field item:voFiles) {
				item.setAccessible(true);
				for(Field field:poFiles) {
					if(item.get(this)==null || "".equals(item.get(this))) {
						continue;
					}
					if(field.getName().equals(item.getName())) {
						field.setAccessible(true);
						String type = field.getType().getSimpleName();
						switch(type) {
						case "String":
							field.set(po, String.valueOf(item.get(this)));
							break;
						case "Date":
							field.set(po, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
									.parse(String.valueOf(item.get(this))));
							break;
						case "Integer":
							field.set(po, Integer.valueOf((String) item.get(this)));
							break;
						case "Short":
							field.set(po, Short.valueOf((String) item.get(this)));
							break;
						case "Double":
							field.set(po, Double.valueOf((String) item.get(this)));
							break;
						case "Boolean":
							field.set(po, Boolean.valueOf((boolean) item.get(this)));
							break;
						case "Long":
							field.set(po, Long.valueOf((String) item.get(this)));
							break;
						default:
							break;
						}
					}
				}
				
			}
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return po;
	}
}
